// import Vue from 'vue'
import {
    Message
} from 'element-ui';
import router from '../router'
import {
    Loading
} from 'element-ui';
import axios from 'axios'

axios.defaults.withCredentials = true;
// Vue.prototype.$message = Message
const request = axios.create({
    baseURL: window._apiUrl.webUrl,
    timeout: 50000,
    headers: {
        'Content-type': 'application/json',
    }
})
//form request
const requestByForm = axios.create({
    baseURL: window._apiUrl.webUrl,
    timeout: 20000,
    headers: {
        'Content-Type': 'multipart/form-data'
    }
})
let option = {
    fullscreen: true,
    background: "rgba(0,0,0,0.8)",
    text: "加载中",
    spinner: "el-icon-loading"
}
export function get(url, params) {
    let loadingInstance = Loading.service(option);
    request.defaults.headers.Authorization = sessionStorage.getItem('token');
    return new Promise((resolve, reject) => {
        request.get(url, {
            params: params
        }).then(res => {
            resolve(res.data);
            if (url != '/captcha' && url != '/forgetPassword' && url != '/verForgetPassword' && url != '/logout') {
                keepAlive();
            }
            loadingInstance.close();
        }).catch(err => {
            reject(err.data)
            loadingInstance.close();
        })
    });
}

export function rest(url, params) {
    request.defaults.headers.Authorization = sessionStorage.getItem('token');
    let loadingInstance = Loading.service(option);
    return new Promise((resolve, reject) => {
        request.get(url + '/' + params).then(res => {
            resolve(res.data);
            loadingInstance.close();
        }).catch(err => {
            reject(err.data)
            loadingInstance.close();
        })
    });
}

export function post(url, params) {
    request.defaults.headers.Authorization = sessionStorage.getItem('token');
    let loadingInstance = Loading.service(option);
    return new Promise((resolve, reject) => {
        request.post(url, JSON.stringify(params))
            .then(res => {
                resolve(res.data);
                if (url != '/user/register') {
                    keepAlive();
                }
                loadingInstance.close();
            })
            .catch(err => {
                reject(err.data)
                loadingInstance.close();
            })
    });
}

export function formPost(url, formData) {
    let loadingInstance = Loading.service(option);
    return new Promise((resolve, reject) => {
        requestByForm.post(url, formData).then(res => {
            resolve(res)
            loadingInstance.close();
        }).catch(res => {
            reject(res.data)
            loadingInstance.close();
        })
    });
}

function keepAlive() {
    if (sessionStorage.getItem("token") != null) {
        request.get('/keepAlive').then(res => {
            console.log("保活机制数据", res.data)
            sessionStorage.setItem("token", res.data.data)
        }).catch(err=>{
            console.log(err)
        })
    }

}

request.interceptors.response.use(
    response => {
        if (response.data.code == 403 || response.data.code == 401) {
            let option = {
                message: response.data.message,
                type: 'error',
            }
            Message(option)
            router.push('/login')
        }
        return response;
    }
)